<template>
  <div class="container">
    <header>
      <van-nav-bar
        title="新增工程验收节点"
        left-arrow
        @click-left="$router.back()">
      </van-nav-bar>
    </header>
    <div class="wd-scroll-body2">
      <div class="wd-group">
        <div class="wd-group__title">形象进度确认</div>
        <div class="wd-group__body">
          <van-field
            label-width="120px"
            label="形象进度确认表">
            <input
              readonly="readonly"
              v-model="xxjdqrb"
              placeholder="选择文件"
              @click="$refs.xxjdqrbFile.click()"
              slot="input"
              type="text"
              class="van-field__control">
            <div class="wd-upload-file__control" slot="button">
              <van-icon @click="upFile(1)" name="upgrade"/>
              <van-icon @click="delFile(1)" name="close"/>
            </div>
          </van-field>
          <van-field
            label-width="120px"
            label="形象进度照片">
            <input
              readonly="readonly"
              v-model="xxjdzp"
              placeholder="选择文件"
              @click="$refs.xxjdzpFile.click()"
              slot="input"
              type="text"
              class="van-field__control">
            <div class="wd-upload-file__control" slot="button">
              <van-icon @click="upFile(2)" name="upgrade"/>
              <van-icon @click="delFile(2)" name="close"/>
            </div>
          </van-field>
        </div>
      </div>
      <div class="wd-group">
        <div class="wd-group__title">质量安全验收确认</div>
        <div class="wd-group__body">
          <van-field
            label-width="120px"
            label="质量验收确认表">
            <input
              readonly="readonly"
              v-model="zjysqrb"
              placeholder="选择文件"
              @click="$refs.zjysqrbFile.click()"
              slot="input"
              type="text"
              class="van-field__control">
            <div class="wd-upload-file__control" slot="button">
              <van-icon @click="upFile(3)" name="upgrade"/>
              <van-icon @click="delFile(3)" name="close"/>
            </div>
          </van-field>
        </div>
      </div>
      <div class="wd-group">
        <div class="wd-group__title">节点完成申报审批审查项</div>
        <div class="wd-group__body">
          <van-cell @click="sbspClick('nodeReport')" class="wd-cell-checkbox" title="工程节点是否汇报完成">
            <i v-show="form.nodeReport == '1' || form.nodeReport == '_1'" class="van-icon van-icon-success"></i>
          </van-cell>
          <van-cell @click="sbspClick('qualityReport')" class="wd-cell-checkbox" title="质量管理整改完成率是否100%">
            <i v-show="form.qualityReport == '1' || form.qualityReport == '_1'" class="van-icon van-icon-success"></i>
          </van-cell>
          <van-cell @click="sbspClick('securityReport')" class="wd-cell-checkbox" title="安全管理整改完成率是否100%">
            <i v-show="form.securityReport == '1' || form.securityReport == '_1'" class="van-icon van-icon-success"></i>
          </van-cell>
          <van-cell @click="sbspClick2('jypjfx')" class="wd-cell-checkbox" title="检验批（隐藏）及分项资料是否移交总包">
            <i v-show="form2.jypjfx == '1'" class="van-icon van-icon-success"></i>
          </van-cell>
          <van-cell @click="sbspClick2('dfbdwcf')" class="wd-cell-checkbox" title="对分包单位的罚款是否执行落地">
            <i v-show="form2.dfbdwcf == '1'" class="van-icon van-icon-success"></i>
          </van-cell>
          <van-cell @click="sbspClick2('jgcclfy')" class="wd-cell-checkbox" title="甲供材超量费用是否已扣减">
            <i v-show="form2.jgcclfy == '1'" class="van-icon van-icon-success"></i>
          </van-cell>
        </div>
      </div>
    </div>
    <van-row style="padding: 10px 0 20px 0">
      <van-col span="18" offset="3">
        <van-button :loading="qrysLoading" @click="qrysClick" :disabled="cYsan" round block type="info">确认验收</van-button>
      </van-col>
    </van-row>
    <input ref="xxjdqrbFile" @change="xxjdqrbChange" class="wd-input-file" type="file">
    <input ref="xxjdzpFile" @change="xxjdzpChange"  class="wd-input-file" type="file">
    <input ref="zjysqrbFile" @change="zjysqrbChange"  class="wd-input-file" type="file">
    <van-overlay :show="updateting">
      <div style="display: flex; align-items: center; justify-content: center; height: 100%;">
        <van-loading type="spinner" />
      </div>
    </van-overlay>
  </div>
</template>

<script>
  import {
    apiGetDetail,
    apiConfirmYS,
    apiSetItems,
    apiGetFileInfo,
    apiUpdate,
    apiGetUploadCode,
    apiUploadBase
  } from '@/api/'
  import moment from 'moment'
  const userNo = localStorage.getItem('userID')
  const addedID = localStorage.getItem('addedID')
  export default {
    data() {
      return {
        updateting: false,
        xxjdqrb: '',
        xxjdqrbFile: null,
        xxjdzp: '',
        xxjdzpFile: null,
        zjysqrb: '',
        zjysqrbFile: null,
        subData: {},
        nodeWcList: [],
        form2: {
          jypjfx: '0',
          dfbdwcf: '0',
          jgcclfy: '0'
        },
        form: {
          figureTable: '',
          figurePhoto: '',
          reviewTable: '',
          nodeReport: '0',
          qualityReport: '0',
          securityReport: '0'
        },
        qrysLoading: false
      }
    },
    // 获取详情数据
    created() {
      const vm = this
      vm.updateting = true

      apiGetDetail(userNo, addedID).then(res => {
        const
          form = vm.form,
          data = res.data

        if (res.code == 2000) {
          for (let p in form) form[p] = res.data[p]

          if (data.reviewDate)
            data.reviewDate = moment(data.reviewDate.time).format('YYYY-MM-DD')

          vm.nodeWcList = data.itemList

          delete data.proNodeList
          delete data.itemList

          vm.subData = data

          vm.nodeWcList.forEach(item => {
            if (item.itemContent == '检验批（隐蔽）及分项资料是否移交总包') {
              vm.form2.jypjfx = item.status
            }
            if (item.itemContent == '对分包单位的的罚款是否已执行落地') {
              vm.form2.dfbdwcf = item.status
            }
            if (item.itemContent == '甲供材超量费用是否已扣减') {
              vm.form2.jgcclfy = item.status
            }
          })

          vm.getFilePath('figureTable')
          vm.getFilePath('figurePhoto')
          vm.getFilePath('reviewTable')
        }
        vm.updateting = false
      })
    },
    computed: {
      cYsan() {
        return (
          (this.form.nodeReport !== '1' && this.form.nodeReport !== '_1') ||
          (this.form.qualityReport !== '1' && this.form.qualityReport !== '_1') ||
          (this.form.securityReport !== '1' && this.form.securityReport !== '_1') ||
          this.form2.jypjfx != '1' ||
          this.form2.dfbdwcf != '1' ||
          this.form2.jgcclfy != '1'
        )
      }
    },
    methods: {
      qrysClick() {
        this.qrysLoading = true

        apiConfirmYS(userNo, addedID).then(res => {
          if (res.code == 2000) {
            window.open(res.url.substr(9), '_self')
          } else {
            this.$toast('验收失败!')
          }
          this.qrysLoading = false
        })
      },
      sbspClick2(name) {
        var
          vm = this,
          sData = null,
          form2 = vm.form2

        function getItem(name) {
          var item = null
          vm.nodeWcList.forEach(node => {
            if (node.itemContent == name) item = node
          })
          return item
        }

        switch(name) {
          case 'jypjfx':
            form2.jypjfx = form2.jypjfx == '1' ? '0' : '1'
            let node = getItem('检验批（隐蔽）及分项资料是否移交总包')
            node.status = form2.jypjfx
            sData = node
          break
          case 'dfbdwcf':
            form2.dfbdwcf = form2.dfbdwcf == '1' ? '0' : '1'
            let node2 = getItem('对分包单位的的罚款是否已执行落地')
            node2.status = form2.dfbdwcf
            sData = node2
          break
          case 'jgcclfy':
            form2.jgcclfy = form2.jgcclfy == '1' ? '0' : '1'
            let node3 = getItem('甲供材超量费用是否已扣减')
            node3.status = form2.jgcclfy
            sData = node3
          break
        }

        vm.updateting = true

        apiSetItems(userNo, sData).then(res => {
          if (res.code == 2000) {
            vm.$toast('更新成功！')
          }
          vm.updateting = false
        })
      },
      delFile(index) {
        const vm = this
        if (index == 1) {
          if (!vm.form.figureTable) {
            vm.$toast('没有上传过文件！')
            return
          }
          vm.xxjdqrbFile = null
          vm.xxjdqrb = ''
          vm.form.figureTable = ''
          vm.subData.figureTable = ''
        }
        if (index == 2) {
          if (!vm.form.figurePhoto) {
            vm.$toast('没有上传过文件！')
            return
          }
          vm.xxjdzp = ''
          vm.xxjdzpFile = null
          vm.form.figurePhoto = ''
          vm.subData.figurePhoto = ''
        }
        if (index == 3) {
          if (!vm.form.reviewTable) {
            vm.$toast('没有上传过文件！')
            return
          }
          vm.zjysqrb = ''
          vm.zjysqrbFile = null
          vm.form.reviewTable = ''
          vm.subData.reviewTable = ''
        }
        vm.updateting = true
        vm.submit()
      },
      getFilePath(name) {
        const
          vm = this,
          code = vm.form[name]

        if (!code) return

        apiGetFileInfo(userNo, code).then(res => {
          if (res.code == 2000) {
            if (name == 'figureTable') {
              vm.xxjdqrb = res.data.attachment.name
            }
            if (name == 'figurePhoto') {
              vm.xxjdzp = res.data.attachment.name
            }
            if (name == 'reviewTable') {
              vm.zjysqrb = res.data.attachment.name
            }
          }
        })
      },
      sbspClick(name) {
        const
          vm = this,
          form = vm.form,
          subData = vm.subData

        if (name == 'nodeReport') {
          form.nodeReport = (form.nodeReport == '1' || form.nodeReport == '_1') ? form.nodeReport = '_0' : form.nodeReport = '_1'
          subData.nodeReport = form.nodeReport
        }
        if (name == 'qualityReport') {
          form.qualityReport = (form.qualityReport == '1' || form.qualityReport == '_1') ? form.qualityReport = '_0' : form.qualityReport = '_1'
          subData.qualityReport = form.qualityReport
        }
        if (name == 'securityReport') {
          form.securityReport = (form.securityReport == '1' || form.securityReport == '_1') ? form.securityReport = '_0' : form.securityReport = '_1'
          subData.securityReport = form.securityReport
        }

        vm.updateting = true
        vm.submit()
      },
      xxjdqrbChange(ev) {
        const file = ev.target.files[0]
        this.xxjdqrb = file.name
        this.xxjdqrbFile = file
      },
      xxjdzpChange(ev) {
        const file = ev.target.files[0]
        this.xxjdzp = file.name
        this.xxjdzpFile = file
      },
      zjysqrbChange(ev) {
        const file = ev.target.files[0]
        this.zjysqrb = file.name
        this.zjysqrbFile = file
      },
      submit() {
        apiUpdate(userNo, this.subData).then(res => {
          if (res.code == 2000) {
            this.$toast('更新成功！')
            this.getFilePath('figureTable')
            this.getFilePath('figurePhoto')
            this.getFilePath('reviewTable')
          } else {
            this.$toast('更新失败！')
          }
          this.updateting = false
        })
      },
      upFile(type) {
        var file = null

        const
          vm = this,
          reader = new FileReader()

        if (type == 1) file = vm.xxjdqrbFile
        if (type == 2) file = vm.xxjdzpFile
        if (type == 3) file = vm.zjysqrbFile

        if (!file) {
          vm.$toast('请选择文件！')
          return
        }

        vm.updateting = true

        reader.onload = function(ev) {
          apiGetUploadCode(userNo).then(res => {
            if (res.code == 2000) {
              const code = res.data
              apiUploadBase(userNo, code, ev.target.result).then(res => {
                if (res.success) {
                  if (type == 1) {
                    vm.form.figureTable = code
                    vm.subData.figureTable = code
                  }
                  if (type == 2) {
                    vm.form.figurePhoto = code
                    vm.subData.figurePhoto = code
                  }
                  if (type == 3) {
                    vm.form.reviewTable = code
                    vm.subData.reviewTable = code
                  }
                  vm.submit()
                } else {
                  vm.updateting = false
                  vm.$toast('文件上传失败！')
                }
              })
            } else {
              vm.updateting = false
              vm.$toast('获取code错误！')
            }
          })
        }

        reader.readAsDataURL(file)
      }
    }
  }
</script>